package com.lzc.tlmail.tlmailproduct.service.impl;

import com.lzc.tlmail.tlmailproduct.entity.AttrEntity;
import com.lzc.tlmail.tlmailproduct.service.AttrService;
import com.lzc.tlmail.tlmailproduct.vo.BaseAttrs;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzc.common.utils.PageUtils;
import com.lzc.common.utils.Query;

import com.lzc.tlmail.tlmailproduct.dao.ProductAttrValueDao;
import com.lzc.tlmail.tlmailproduct.entity.ProductAttrValueEntity;
import com.lzc.tlmail.tlmailproduct.service.ProductAttrValueService;
import org.springframework.transaction.annotation.Transactional;


@Service("productAttrValueService")
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity> implements ProductAttrValueService {

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductAttrValueEntity> page = this.page(
                new Query<ProductAttrValueEntity>().getPage(params),
                new QueryWrapper<ProductAttrValueEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存spu基本规格参数
     * @param baseAttrs
     */
    @Override
    public void saveAttrValue(List<BaseAttrs> baseAttrs,Long id) {
        if(baseAttrs.size()>0&&baseAttrs!=null){
            List<ProductAttrValueEntity> productAttrValueEntities = baseAttrs.stream().map(baseAttrs1 -> {
                ProductAttrValueEntity productAttrValueEntity = new ProductAttrValueEntity();
                productAttrValueEntity.setAttrId(baseAttrs1.getAttrId());
                AttrEntity attrEntity = attrService.getById(baseAttrs1.getAttrId());
                productAttrValueEntity.setAttrName(attrEntity.getAttrName());
                productAttrValueEntity.setAttrValue(baseAttrs1.getAttrValues());
                productAttrValueEntity.setQuickShow(baseAttrs1.getShowDesc());
                productAttrValueEntity.setSpuId(id);
                return productAttrValueEntity;
            }).collect(Collectors.toList());
            this.saveBatch(productAttrValueEntities);
        }
    }

    /**
     * 获取spu的规格参数
     * @param spuId
     * @return
     */
    @Override
    public List<ProductAttrValueEntity> getAttrBySpuId(Long spuId) {

        List<ProductAttrValueEntity> productAttrValueEntities = this.list(new QueryWrapper<ProductAttrValueEntity>()
        .eq("spu_id",spuId));
        return productAttrValueEntities;
    }

    /**
     * 修改spu对应的规格参数
     * @param spuId
     * @param productAttrValueEntities
     */
    @Transactional
    @Override
    public void updateSpuAttr(Long spuId, List<ProductAttrValueEntity> productAttrValueEntities) {
        this.remove(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id",spuId));

        List<ProductAttrValueEntity> productAttrValueEntities1 = productAttrValueEntities.stream().map(productAttrValueEntity -> {
            productAttrValueEntity.setSpuId(spuId);
            return productAttrValueEntity;
        }).collect(Collectors.toList());

        this.saveBatch(productAttrValueEntities1);
    }

}